package com.javasm.service.impl;

import com.javasm.dao.GiftInfoDao;
import com.javasm.entity.GiftInfo;
import com.javasm.service.GiftInfoService;
import com.javasm.utils.MyBatisHeapler;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class GiftInfoServiceImpl implements GiftInfoService {

    @Override
    public Integer addGift(GiftInfo inputGift) {
        SqlSession sqlSession = MyBatisHeapler.getSqlSession();
        GiftInfoDao mapper = sqlSession.getMapper(GiftInfoDao.class);
        Integer resNum = mapper.addGift(inputGift);
        MyBatisHeapler.backAndSaveSqlSession(sqlSession);
        return resNum;
    }

    @Override
    public Integer countGifts(GiftInfo inputGift) {
        SqlSession sqlSession = MyBatisHeapler.getSqlSession();
        GiftInfoDao mapper = sqlSession.getMapper(GiftInfoDao.class);
        Integer resNum = mapper.countGifts(inputGift);
        MyBatisHeapler.backAndSaveSqlSession(sqlSession);

        return resNum;
    }

    @Override
    public Integer deleteGiftById(Integer giftId) {
        SqlSession sqlSession = MyBatisHeapler.getSqlSession();
        GiftInfoDao mapper = sqlSession.getMapper(GiftInfoDao.class);
        Integer resNum = mapper.deleteGiftById(giftId);

        MyBatisHeapler.backAndSaveSqlSession(sqlSession);

        return resNum;
    }

    @Override
    public Integer editGift(GiftInfo inputGift) {
        SqlSession sqlSession = MyBatisHeapler.getSqlSession();
        GiftInfoDao mapper = sqlSession.getMapper(GiftInfoDao.class);
        Integer resNum = mapper.editGift(inputGift);
        MyBatisHeapler.backAndSaveSqlSession(sqlSession);
        return resNum;

    }

    @Override
    public GiftInfo getGiftById(Integer giftId) {
        SqlSession sqlSession = MyBatisHeapler.getSqlSession();
        GiftInfoDao mapper = sqlSession.getMapper(GiftInfoDao.class);
        GiftInfo giftInfo = mapper.getGiftById(giftId);
        MyBatisHeapler.backAndSaveSqlSession(sqlSession);
        return giftInfo;
    }

    @Override
    public List<GiftInfo> listAllGift(GiftInfo inputGift, Integer page, Integer pageSize) {

        SqlSession sqlSession = MyBatisHeapler.getSqlSession();
        GiftInfoDao mapper = sqlSession.getMapper(GiftInfoDao.class);
        List<GiftInfo> giftInfoList = mapper.listAllGift(inputGift, (page - 1) * pageSize, pageSize);
        MyBatisHeapler.backAndSaveSqlSession(sqlSession);
        return giftInfoList;
    }
}
